<template>
  <el-dialog title="新增员工" :visible="showDialog" top="8vh" :close-on-click-modal="false" @click.native="tree" @close="closeDialog">
    <!-- 表单 -->
    <el-form ref="form" label-width="120px" :model="ruleForm" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="ruleForm.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="ruleForm.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="ruleForm.timeOfEntry" value-format="yyyy-MM-dd" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="ruleForm.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option v-for="item in employees" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="ruleForm.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item v-loading="loading" label="部门" prop="departmentName">
        <el-input :value="ruleForm.departmentName" style="width:50%" placeholder="请选择部门" @click.native.stop="getDepartmentList" />
        <!-- 这里是显示部门的盒子 -->
        <div v-if="show" class="tree-box">
          <el-tree :data="DepartmentList" :props="{ label: 'name' }" @node-click="nodeClick" />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="ruleForm.correctionTime" style="width:50%" value-format="yyyy-MM-dd" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="addDepartment">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartmentListApi } from '@/api/departments'
import { addEmployeesSimleApi } from '@/api/employees'
import { transListToTreeData } from '@/utils/index'
import employees from '@/constant/employees'
export default {
  name: 'ComEmp',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择日期', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用格式', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '请输入工号', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '请选择部门', trigger: ['blur', 'change'] }
        ],
        correctionTime: [
          { required: true, message: '请填写转正时间', trigger: ['blur', 'change'] }
        ]
      },
      DepartmentList: [],
      // 显示部门树
      show: false,
      loading: false,
      employees: employees.hireType
    }
  },
  methods: {
    // 添加员工
    addDepartment() {
      this.$refs.form.validate(async flag => {
        if (!flag) return
        await addEmployeesSimleApi(this.ruleForm)
        this.$message.success('添加成功')
        this.closeDialog()
        this.getDepartmentList()
      })
    },
    // 点击树 数据回显到输入框
    nodeClick(data) {
      if (data.children.length > 0) return
      this.ruleForm.departmentName = data.name
      this.show = false
    },
    // 控制部门树显示
    tree() {
      this.show = false
    },
    // 获取列表
    async getDepartmentList() {
      this.show = !this.show
      this.loading = true
      const { data } = await getDepartmentListApi()
      this.DepartmentList = transListToTreeData(data.depts, '')
      this.loading = false
    },
    closeDialog() {
      this.$emit('update:showDialog', false)
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-box {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  overflow: auto;
  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
  }
}
</style>
